package com.sanpu.kxe.business.card.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.contract.AddBankSetupContract;
import com.sanpu.kxe.business.card.presenter.AddBankSetupPresenter;
import com.sanpu.kxe.business.repay.model.RepayModel;
import com.sanpu.kxe.business.repay.provider.RepayOpenProvider;
import com.sanpu.kxe.common.utils.SingleCheckListener;
import com.sanpu.kxe.common.view.BankCardEditText;
import com.sanpu.kxe.module.dialog.HintDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：添加银行卡第一步
 * Created by 薛勇军 on 2018/7/17.
 */
@Route(path = "/bank/addBankAct")
public class AddBankSetupActivity extends BaseTitleActivity implements AddBankSetupContract.View {

    @Bind(R.id.containLayout)
    LinearLayout containLayout;
    @Bind(R.id.name)
    TextView vName;
    @Bind(R.id.bank_number)
    BankCardEditText vBankNumber;
    @Bind(R.id.billDayTv)
    TextView billDayTv;
    @Bind(R.id.refundDayTv)
    TextView refundDayTv;

    private SingleCheckListener singleCheckListener;

    private AddBankSetupContract.Presenter mPresenter;

    private List<String> selectList = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_add_bank_setup;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("添加信用卡");
        commonTitle.showRightTextView("支持银行", ContextCompat.getColor(mContext, R.color.color_base));
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RepayOpenProvider.getInstance().openRepaySupportActivity(mContext, null);
            }
        });

        selectList = RepayModel.getInstance().getSelectList();

        mPresenter = new AddBankSetupPresenter(this);
        mPresenter.initViewData();
    }

    @OnClick({R.id.hint, R.id.next, R.id.billDayLl, R.id.refundDayLl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hint:
                TextView textView = new TextView(mContext);
                textView.setText("为保障账户资金安全，只能绑定认证用户本人的银行卡。");
                new HintDialog(mContext)
                        .title("持卡人说明")
                        .content(textView)
                        .show();
                break;
            case R.id.next:
                mPresenter.onNextClick(vBankNumber.getBankCardText(),
                        billDayTv.getText().toString(), refundDayTv.getText().toString());
                break;
            case R.id.billDayLl:
                if (singleCheckListener == null) {
                    singleCheckListener = new SingleCheckListener(AddBankSetupActivity.this);
                }
                singleCheckListener.handleCheck(containLayout, billDayTv, selectList);
                break;
            case R.id.refundDayLl:
                if (singleCheckListener == null) {
                    singleCheckListener = new SingleCheckListener(AddBankSetupActivity.this);
                }
                singleCheckListener.handleCheck(containLayout, refundDayTv, selectList);
                break;
            default:
                break;
        }
    }

    @Override
    public void showCrpName(String crpName) {
        if (!TextUtils.isEmpty(crpName)) {
            vName.setText(crpName);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AddBankSetupContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
